/* eslint-disable */
// const { username } = require('../rules/user/login');
const { Op } = require('sequelize');

const Service = require('egg').Service;

class UserService extends Service {
    //登录
    async login(username, password) {
        const { ctx, app } = this
        const user = await ctx.model.User.findOne({
            where: { username }
        })
        if (user) {
            const hash = user.password
            const result = await ctx.compare(password, hash);
            if (result) {
                const token = app.jwt.sign({ username }, app.config.jwt.secret);
                return { success: true, data: { userid: user.id, username, token, status: user.status } }
            } else {
                return { success: false, message: '密码不正确' }
            }
        } else {
            return { success: false, message: '用户不存在' }
        }
    }
    //查找用户列表
    async findList(username = '', phone = '', pageIndex = 1, pageSize = 5) {
        const { ctx } = this;
        const offset = (pageIndex - 1) * pageSize;
        const where = {};
        if (username) {
            where.username = { [Op.like]: `%${username}%` };
        }
        if (nickname) {
            where.phone = { [Op.like]: `%${phone}%` };
        }
        return await ctx.model.User.findAndCountAll({
            where,
            attributes: {
                exclude: ['password']
            },
            offset: Number(offset),
            limit: Number(pageSize)
        });
    }

    async findById(id) {
        const { ctx } = this;
        return await ctx.model.User.findByPk(id, {
            attributes: {
                exclude: ['password']
            },
        })
    }
    //检查用户用户名有效性
    async checkUserNameValid(username) {
        const { ctx } = this
        const result = await ctx.model.User.findOne({
            where: { username }
        })
        return result == null ? true : false
    }

    //添加
    async add(user) {
        const { ctx } = this;
        if (typeof user.password !== 'string') {
            ctx.error('密码格式错误，密码必须是字符串！');
            return;
        }
        // console.log('Original Password:', user.password);
        user.password = await ctx.genHash(user.password)
        // console.log('Hashed Password:', user.password); 
        return ctx.model.User.create(user)
    }
    //删除
    async remove(id) {
        const { ctx } = this;
        const affectedRows = await ctx.model.destory({ where: { id } });
        return affectedRows ? true : false;
    }
    //修改
    async modify(id, user) {
        const { ctx } = this;
        const [affectedRows] = await ctx.model.User.update(user, {
            where: { id }
        })
        return affectedRows ? true : false;
    }
    //修改用户状态
    async changeStatus(id, status) {
        const { ctx } = this;
        const user = await ctx.model.User.findByPk(id);
        if (!user) {
            return false;
        }
        user.status = status;
        await user.save();
        return true;
    }


}
module.exports = UserService;